package com.huaxin.hxenum.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;
import com.huaxin.hxenum.base.BaseCodeEnum;

/**
 * 计量单位枚举 (Unit)
 * 用于定义设备监测参数的计量单位
 * 增强版：支持反向关联到 EquipmentDataType 和 DeviceType
 */
public enum Unit implements BaseCodeEnum<String> {

    // -------------------- 电力参数 --------------------
    AMPERE("A", "安培",
            new DeviceType[]{DeviceType.POWER_METER_10KV, DeviceType.POWER_METER_04KV, DeviceType.MICROPROCESSOR_RELAY},
            new EquipmentDataType[]{EquipmentDataType.CURRENT_PHASE_A, EquipmentDataType.CURRENT_PHASE_B, EquipmentDataType.CURRENT_PHASE_C}),

    MILLI_AMPERE("mA", "毫安",
            new DeviceType[]{DeviceType.POWER_METER_10KV, DeviceType.POWER_METER_04KV, DeviceType.MICROPROCESSOR_RELAY},
            null),

    VOLT("V", "伏特",
            new DeviceType[]{DeviceType.POWER_METER_10KV, DeviceType.POWER_METER_04KV, DeviceType.MICROPROCESSOR_RELAY},
            null),

    KILO_VOLT("kV", "千伏",
            new DeviceType[]{DeviceType.POWER_METER_10KV},
            new EquipmentDataType[]{
                    EquipmentDataType.VOLTAGE_PHASE_A,
                    EquipmentDataType.VOLTAGE_PHASE_B,
                    EquipmentDataType.VOLTAGE_PHASE_C,
                    EquipmentDataType.VOLTAGE_LINE_AB,
                    EquipmentDataType.VOLTAGE_LINE_BC,
                    EquipmentDataType.VOLTAGE_LINE_CA
            }),

    VA("VA", "伏安",
            new DeviceType[]{DeviceType.POWER_METER_10KV, DeviceType.POWER_METER_04KV},
            null),

    KILO_VA("kVA", "千伏安",
            new DeviceType[]{DeviceType.POWER_METER_10KV, DeviceType.POWER_METER_04KV},
            new EquipmentDataType[]{EquipmentDataType.APPARENT_POWER_TOTAL}),

    WATT("W", "瓦特",
            new DeviceType[]{DeviceType.POWER_METER_10KV, DeviceType.POWER_METER_04KV, DeviceType.MICROPROCESSOR_RELAY},
            null),

    KILO_WATT("kW", "千瓦",
            new DeviceType[]{DeviceType.POWER_METER_10KV, DeviceType.POWER_METER_04KV, DeviceType.MICROPROCESSOR_RELAY},
            new EquipmentDataType[]{EquipmentDataType.ACTIVE_POWER_TOTAL}),

    KILO_WATT_HOUR("kWh", "千瓦时",
            new DeviceType[]{DeviceType.POWER_METER_10KV, DeviceType.POWER_METER_04KV},
            new EquipmentDataType[]{
                    EquipmentDataType.ENERGY_ACTIVE_FORWARD,
                    EquipmentDataType.ENERGY_ACTIVE_REVERSE,
                    EquipmentDataType.ENERGY_REACTIVE_FORWARD,
                    EquipmentDataType.ENERGY_REACTIVE_REVERSE
            }),

    VAR("var", "乏",
            new DeviceType[]{DeviceType.POWER_METER_10KV, DeviceType.POWER_METER_04KV},
            null),

    KILO_VAR("kvar", "千乏",
            new DeviceType[]{DeviceType.POWER_METER_10KV, DeviceType.POWER_METER_04KV},
            new EquipmentDataType[]{
                    EquipmentDataType.REACTIVE_POWER_TOTAL
            }),

    POWER_FACTOR("", "功率因数",
            new DeviceType[]{DeviceType.POWER_METER_10KV, DeviceType.POWER_METER_04KV},
            new EquipmentDataType[]{EquipmentDataType.POWER_FACTOR_TOTAL}),

    HERTZ("Hz", "赫兹",
            new DeviceType[]{DeviceType.POWER_METER_10KV, DeviceType.POWER_METER_04KV},
            new EquipmentDataType[]{EquipmentDataType.FREQUENCY}),

    // -------------------- 环境参数 --------------------
    CELSIUS("°C", "摄氏度",
            new DeviceType[]{DeviceType.ENV_MONITOR_ROOM, DeviceType.TRANSFORMER_TEMP_CONTROLLER, DeviceType.WIRELESS_TEMP_HOST},
            new EquipmentDataType[]{
                    EquipmentDataType.TEMPERATURE_ROOM,
                    EquipmentDataType.TEMPERATURE_TRANSFORMER_A,
                    EquipmentDataType.TEMPERATURE_TRANSFORMER_B,
                    EquipmentDataType.TEMPERATURE_TRANSFORMER_C,
                    EquipmentDataType.TEMPERATURE_CABLE_A,
                    EquipmentDataType.TEMPERATURE_CABLE_B,
                    EquipmentDataType.TEMPERATURE_CABLE_C
            }),

    DEGREE_C("℃", "摄氏度",
            new DeviceType[]{DeviceType.ENV_MONITOR_ROOM, DeviceType.TRANSFORMER_TEMP_CONTROLLER, DeviceType.WIRELESS_TEMP_HOST},
            new EquipmentDataType[]{
                    EquipmentDataType.TEMPERATURE_ROOM,
                    EquipmentDataType.TEMPERATURE_TRANSFORMER_A,
                    EquipmentDataType.TEMPERATURE_TRANSFORMER_B,
                    EquipmentDataType.TEMPERATURE_TRANSFORMER_C,
                    EquipmentDataType.TEMPERATURE_CABLE_A,
                    EquipmentDataType.TEMPERATURE_CABLE_B,
                    EquipmentDataType.TEMPERATURE_CABLE_C
            }),

    PERCENT("%", "百分比",
            new DeviceType[]{
                    DeviceType.POWER_METER_10KV,
                    DeviceType.POWER_METER_04KV
            },
            null),
    PERCENT_RH("%RH", "相对湿度",
            new DeviceType[]{
                    DeviceType.ENV_MONITOR_ROOM,
            },
            new EquipmentDataType[]{
                    EquipmentDataType.HUMIDITY_ROOM
            }),

    MICROGRAMS_PER_CUBIC_METER("μg/m³", "微克/立方米",
            new DeviceType[]{DeviceType.ENV_MONITOR_ROOM},
            null),

    MILLIGRAMS_PER_CUBIC_METER("mg/m³", "毫克/立方米",
            new DeviceType[]{DeviceType.ENV_MONITOR_ROOM},
            null),

    PPM("ppm", "ppm",
            new DeviceType[]{DeviceType.ENV_MONITOR_ROOM},
            null),

    DECIBEL("dB", "分贝",
            new DeviceType[]{DeviceType.ENV_MONITOR_ROOM},
            null),

    MILLIMETER("mm", "毫米",
            new DeviceType[]{DeviceType.ENV_MONITOR_ROOM},
            null),

    CENTIMETER("cm", "厘米",
            new DeviceType[]{DeviceType.ENV_MONITOR_ROOM},
            null),
    /**
     * 毫米/秒 - 用于振动速度监测
     * 通常由振动传感器通过模拟量模块上传
     */
    MILLIMETER_PER_SECOND("mm/s", "毫米/秒",
            new DeviceType[]{DeviceType.ACTIVE_SIGNAL_MODULE, DeviceType.ENV_MONITOR_ROOM},
            null),

    // -------------------- 通用单位 --------------------

    /**
     * 秒 - 时间持续类参数单位
     */
    SECOND("s", "秒",
            new DeviceType[]{
                    DeviceType.POWER_METER_10KV,
                    DeviceType.POWER_METER_04KV,
                    DeviceType.ENV_MONITOR_ROOM,
                    DeviceType.MICROPROCESSOR_RELAY
            },
            null),

    /**
     * 分钟 - 时间持续类参数单位
     */
    MINUTE("min", "分钟",
            new DeviceType[]{
                    DeviceType.POWER_METER_10KV,
                    DeviceType.POWER_METER_04KV,
                    DeviceType.ENV_MONITOR_ROOM,
                    DeviceType.MICROPROCESSOR_RELAY
            },
            null),

    /**
     * 小时 - 时间持续类参数单位
     */
    HOUR("h", "小时",
            new DeviceType[]{
                    DeviceType.POWER_METER_10KV,
                    DeviceType.POWER_METER_04KV,
                    DeviceType.ENV_MONITOR_ROOM,
                    DeviceType.MICROPROCESSOR_RELAY
            },
            null),

    /**
     * 次 - 计数类参数，如故障次数、动作次数
     */
    TIMES("", "次",
            new DeviceType[]{
                    DeviceType.PASSIVE_SIGNAL_MODULE,
                    DeviceType.ACTIVE_SIGNAL_MODULE,
                    DeviceType.ENV_MONITOR_ROOM,
                    DeviceType.MICROPROCESSOR_RELAY
            },
            null),

    /**
     * 无 - 用于状态量、布尔量等无量纲参数
     */
    NONE("", "无",
            new DeviceType[]{
                    DeviceType.PASSIVE_SIGNAL_MODULE,
                    DeviceType.ENV_MONITOR_ROOM,
                    DeviceType.TRANSFORMER_TEMP_CONTROLLER,
                    DeviceType.WIRELESS_TEMP_HOST,
                    DeviceType.MICROPROCESSOR_RELAY
            },
            null);

    @EnumValue
    private final String code;
    private final String displayName;
    private final DeviceType[] supportedDeviceTypes;
    private final EquipmentDataType[] supportedDataTypes;

    Unit(String code, String displayName, DeviceType[] supportedDeviceTypes, EquipmentDataType[] supportedDataTypes) {
        this.code = code;
        this.displayName = displayName;
        this.supportedDeviceTypes = supportedDeviceTypes;
        this.supportedDataTypes = supportedDataTypes;
    }

    @JsonValue
    @Override
    public String getCode() {
        return code;
    }

    @Override
    public String getDisplayName() {
        return displayName;
    }

    public DeviceType[] getSupportedDeviceTypes() {
        return supportedDeviceTypes;
    }

    public EquipmentDataType[] getSupportedDataTypes() {
        return supportedDataTypes;
    }

    /**
     * 根据 code 或 displayName 获取对应的枚举实例
     */
    public static Unit of(String code) {
        if (code == null || code.trim().isEmpty()) return null;
        for (Unit unit : values()) {
            if (unit.getCode().equalsIgnoreCase(code) ||
                    unit.getDisplayName().equalsIgnoreCase(code.trim())) {
                return unit;
            }
        }
        return null;
    }

    public static boolean isValid(String code) {
        return of(code) != null;
    }

    /**
     * 检查该单位是否适用于指定的设备类型
     */
    public boolean isSupportedBy(DeviceType deviceType) {
        if (deviceType == null || supportedDeviceTypes.length == 0) {
            return true;
        }
        for (DeviceType type : supportedDeviceTypes) {
            if (type == deviceType) {
                return true;
            }
        }
        return false;
    }

    /**
     * 检查该单位是否适用于指定的数据类型
     */
    public boolean isSuitableFor(EquipmentDataType dataType) {
        if (dataType == null || supportedDataTypes.length == 0) {
            return true;
        }
        for (EquipmentDataType type : supportedDataTypes) {
            if (type == dataType) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取所有支持该数据类型的单位
     */
    public static Unit[] getUnitsFor(EquipmentDataType dataType) {
        return java.util.Arrays.stream(values())
                .filter(unit -> unit.isSuitableFor(dataType))
                .toArray(Unit[]::new);
    }
}